package cn.edu.njpi.controller;

import cn.edu.njpi.model.TClass;
import cn.edu.njpi.model.TUser;
import cn.edu.njpi.service.TClassService;
import cn.edu.njpi.utils.InformUtils;
import cn.edu.njpi.utils.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author 朱宗辉
 * @version V1.0
 * @Package cn.edu.njpi.controller
 * @date 2022/4/20 9:19 PM
 */
@RequestMapping(value = "/api/class")
@RestController
public class ClassController {
    @Autowired
    private TClassService tClassService;

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public T addClass(@RequestBody TClass tClass, @RequestHeader("token") String token) {
        T t = new T();
        TUser tUser = (TUser) InformUtils.map.get(token);
        if (tUser.getUserPermssionId() <= 3) {
            t = tClassService.addClass(tClass);
        } else {
            t.add("error", "您无权进行此操作");
            t.setStatus(403);
        }
        return t;
    }

    @RequestMapping(value = "/change", method = RequestMethod.POST)
    public T changeClass(@RequestBody TClass tClass, @RequestHeader("token") String token) {
        T t = new T();
        TUser tUser = (TUser) InformUtils.map.get(token);
        if (tUser.getUserPermssionId() <= 3) {
            t = tClassService.changeClass(tClass);
        } else {
            t.add("error", "您无权进行此操作");
            t.setStatus(403);
        }
        return t;
    }
}
